﻿package we3d.core 
{
	import we3d.core.Camera3d;
	import we3d.core.culling.IObjectCulling;
	import we3d.core.culling.BoxCulling;
	import we3d.math.Matrix3d;
	import we3d.core.transform.Transform3d;
	import flash.utils.getQualifiedClassName;
	
	/**
	* Object3d is the base class for all SceneObjects, Lights, Bones, etc
	*/
	public class Object3d 
	{
		public function Object3d () {}
		
		/**
		* The transform contains the position, rotation and other properties like parent, target and keyframe channels 
		*/
		public var transform:Transform3d  = new Transform3d;
		/**
		* Performs object culling
		*/
		public var objectCuller:IObjectCulling = new BoxCulling;
		/**
		* Read Only, true if the object is culled by the object culler
		*/
		public var culled:Boolean=false;
		/**
		* Frame counter of the last rendered frame
		*/
		public var frameCounter:int=0;
		/**
		* If frameInit is true, the transform will be initialized with initFrame
		*/
		public var frameInit:Boolean=false;
		/**
		* If fastInit is true, the transform will be initialized with initFrame, but only if the object is not culled
		*/
		public var fastInit:Boolean=true;
		
		/**
		* Change the transform type and the available features in the transform (scaling, parenting, keyframing etc.)
		* <code> obj.setTransform ( new we3d.core.transform.Animated() ); </code>
		* @param	tr
		*/
		public function setTransform (tr:Transform3d) :void {

			var classname:String = getQualifiedClassName(tr);
			
			if(classname == "we3d.core.transform::Transform3d") {
				frameInit = false;
			}else{
				frameInit = true;
				fastInit = false;
			}
			transform = tr;
		}
		
		/**
		* @private
		*/
		public function initFrame (f:Number, cam:Camera3d, cam_cgv:Matrix3d) :Boolean {
			
			if((culled = objectCuller.cull(this, cam, cam_cgv)) && fastInit) return true;
			
			frameCounter = Transform3d.FCounter;
			
			if(frameInit) transform.initFrame(f);
			
			return culled;
		}
		
	}
}